package com.sbw.wx.controller;

import com.alibaba.fastjson.JSONObject;
import com.sbw.wx.entity.groupsendmessage.CheckGroupSendMessageBean;
import com.sbw.wx.entity.templatemessage.TemplateMessage;
import com.sbw.wx.path.RequestPathConst;
import com.sbw.wx.utils.AccessTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.text.MessageFormat;

/**
 * Desc: 模板消息
 * @Date: 2021/6/28 8:48
 * @Author: Lee
 * 面朝大海  春暖花开
 * */
@Slf4j
@RestController
@RequestMapping("/templateMessage")
public class TemplateMessageController {

    @Autowired
    private AccessTokenUtil accessTokenUtil;
    @Autowired
    private RestTemplate restTemplate;

    /**
     * Desc: 发送模板消息
     * @Date: 2021/6/28 8:49
     * @Author: Lee
     * 面朝大海  春暖花开
     * */
    @PostMapping("/sendTemplateMessage")
    public String sendTemplateMessage(@RequestBody TemplateMessage templateMessage){
        log.info("发送模板消息开始，方法入参:{}", templateMessage.toString());
        String requestUrl = MessageFormat.format(RequestPathConst.sendTemplateMessage, accessTokenUtil.getAccessToken());
        log.info("发送模板消息请求Url:{}", requestUrl);
        String requestJson = JSONObject.toJSONString(templateMessage);
        log.info("发送模板消息请求参数:{}", requestJson);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity(requestJson, headers);
        ResponseEntity<String> responseEntityStr = restTemplate.postForEntity(requestUrl, request, String.class);
        log.info("发送模板消息响应:{}", responseEntityStr.getBody());
        JSONObject retObj = JSONObject.parseObject(responseEntityStr.getBody());
        if (0 == retObj.getInteger("errcode")) {
            log.info("发送模板消息返回结果是:{}", retObj.toString());
            return retObj.toString();
        }
        log.error("发送模板消息异常，异常Code:{}, 异常信息:{}", retObj.get("errcode"), retObj.getString("errmsg"));
        return "";
    }

    /**
     * Desc: 获取模板列表
     * @Date: 2021/6/28 20:59
     * @Author: Lee
     * 面朝大海  春暖花开
     * */
    @GetMapping("/getTemplateList")
    public String getTemplateList(){
        log.info("获取模板列表开始");
        String requestUrl = MessageFormat.format(RequestPathConst.getTemplateList, accessTokenUtil.getAccessToken());
        log.info("获取模板列表请求Url:{}", requestUrl);
        ResponseEntity<String> responseEntityStr = restTemplate.getForEntity(requestUrl, String.class);
        log.info("获取模板列表响应:{}", responseEntityStr.getBody());
        JSONObject retObj = JSONObject.parseObject(responseEntityStr.getBody());
        if (!StringUtils.isEmpty(retObj.getString("template_list"))) {
            log.info("获取模板列表返回结果是:{}", retObj.toString());
            return retObj.toString();
        }
        log.error("获取模板列表异常，异常Code:{}, 异常信息:{}", retObj.get("errcode"), retObj.getString("errmsg"));
        return "";
    }

    /**
     * Desc: 删除模板消息
     * @Date: 2021/6/28 20:59
     * @Author: Lee
     * 面朝大海  春暖花开
     * */
    @DeleteMapping("/deleteTemplateMessage/{templateId}")
    public String sendTemplateMessage(@PathVariable("templateId") String templateId){
        log.info("删除模板开始，方法入参:{}", templateId);
        String requestUrl = MessageFormat.format(RequestPathConst.deleteTemplateMessage, accessTokenUtil.getAccessToken());
        log.info("删除模板请求Url:{}", requestUrl);
        JSONObject requestJson = new JSONObject();
        requestJson.put("template_id", templateId);
        log.info("删除模板请求参数:{}", requestJson.toJSONString());
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity(requestJson, headers);
        ResponseEntity<String> responseEntityStr = restTemplate.postForEntity(requestUrl, request, String.class);
        log.info("删除模板响应:{}", responseEntityStr.getBody());
        JSONObject retObj = JSONObject.parseObject(responseEntityStr.getBody());
        if (0 == retObj.getInteger("errcode")) {
            log.info("删除模板返回结果是:{}", retObj.toString());
            return retObj.toString();
        }
        log.error("删除模板异常，异常Code:{}, 异常信息:{}", retObj.get("errcode"), retObj.getString("errmsg"));
        return "";
    }

}
